﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MainQuoteDBModel;
using CMSModel;

public partial class Topics_ConcernedPlate_edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.IsPostBack)
        {
            bd();
        }
    }

    private void bd()
    {
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
        ddlBkAdd.DataSource = dal.TB_BK.Where(x => x.BK_CODE.StartsWith("002"));
        ddlBkAdd.DataTextField = "BK_NAME";
        ddlBkAdd.DataValueField = "BK_CODE";
        ddlBkAdd.DataBind();
        ddlGNBk.DataSource = dal.TB_BK.Where(x => x.BK_CODE.StartsWith("001"));
        ddlGNBk.DataTextField = "BK_NAME";
        ddlGNBk.DataValueField = "BK_CODE";
        ddlGNBk.DataBind();

        var id = Guid.Parse(Request["id"]);
        CMSEntities cms = new CMSEntities();
        var str= cms.Topics.First(x => x.ID == id).Content;
        var tt=  Newtonsoft.Json.JsonConvert.DeserializeObject<List<Gzbk>>(str);
        if (tt.Count > 0)
        {
            var temp = tt[0];
            hidBkId.Value = temp.BKID;
            lblBkName.Text = temp.BKName;
            txtTitle.Text = temp.News.Title;
            txtSummary.Text = temp.News.Content;
            try
            {
                var news = cms.News.First(x => x.id == temp.News.Id);
                txtContent.Text = news.content;
                txtKeyword.Text = news.keyword;
            }
            catch
            {
            }
            if (temp.Stocks != null)
            {
                string ss = "";
                foreach (var c in temp.Stocks)
                {
                    ss += c.Code+",";
                }
                if (ss.Length > 0)
                {
                    ss = ss.Substring(0, ss.Length - 1);
                }
                txtCode.Text = ss;
            }
            
        }

    }

    protected void btnGaiNian_Click(object sender, EventArgs e)
    {
        hidBkId.Value = ddlGNBk.SelectedValue;
        lblBkName.Text = ddlGNBk.SelectedItem.Text;
        var bkid = ddlGNBk.SelectedValue;
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
        var stocks = dal.TB_BK_GG.Where(x => x.BK_CODE == bkid).Select(x => x.GG_CODE).ToList();
    }

    protected void btnBkAdd_Click(object sender, EventArgs e)
    {
        hidBkId.Value = ddlBkAdd.SelectedValue;
        lblBkName.Text = ddlBkAdd.SelectedItem.Text;
        var bkid = ddlBkAdd.SelectedValue;
        MainQuoteDBEntities dal = new MainQuoteDBEntities();
        var stocks = dal.TB_BK_GG.Where(x => x.BK_CODE == bkid).Select(x => x.GG_CODE).ToList();
      
    }
 

 
    protected void btnSaveToDB_Click(object sender, EventArgs e)
    {
        if (string.IsNullOrEmpty(hidBkId.Value))
        {
            ClientScript.RegisterStartupScript(GetType(), "aa", "alert('没有选择板块')", true);
            return;
        }
        CMSEntities cms = new CMSEntities();
        var id = Guid.Parse(Request["id"]);
        CMSModel.Topics topics = cms.Topics.First(x => x.ID == id);
        List<Gzbk> data = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Gzbk>>(topics.Content);
        if (data.Count == 0)
        {
            data.Add(new Gzbk());
            
        }
        Gzbk gzbk = data[0];
        gzbk.BKID = hidBkId.Value;
        gzbk.BKName = lblBkName.Text;
        if (gzbk.News == null)
        {
            gzbk.News = new Gzbk_News();
        }
        gzbk.News.Title = txtTitle.Text;
        gzbk.News.Content = txtSummary.Text;
        gzbk.Stocks = new List<Gzbk_Stock>();
        foreach (var c in txtCode.Text.Split(',', '，'))
        {
            Gzbk_Stock gs = new Gzbk_Stock();
            gs.Code = c;
            try
            {
                gs.Name = CommonBll.DicStocks[c];
            }
            catch
            {
                ClientScript.RegisterStartupScript(GetType(), "aa", "alert('" + c + "股票错误！')", true);
                return;
            }
            gzbk.Stocks.Add(gs);
        }
        //更新新闻
        {
            if (gzbk.News.Id == null || gzbk.News.Id == 0)
            {
                CMSEntities cms2 = new CMSEntities();
                //先把新闻保存进数据库 
                News news = new News();
                news.title = txtTitle.Text;
                news.keyword = txtKeyword.Text;
                news.IsEnable = true;
                news.ptime = DateTime.Now;
                news.Summary = txtSummary.Text;
                news.content = txtContent.Text;
                cms2.News.AddObject(news);
                cms2.SaveChanges();
                CacheClearBLL.NewsClear(news.id);
                gzbk.News.Id = news.id;
            }
            else
            {
                News news = cms.News.First(x => x.id == gzbk.News.Id);
                news.title = txtTitle.Text;
                news.keyword = txtKeyword.Text;
                news.Summary = txtSummary.Text;
                news.content = txtContent.Text;
            }
        }
        topics.Content = Newtonsoft.Json.JsonConvert.SerializeObject(data);
        cms.SaveChanges();
        CacheClearBLL.NewsClear(gzbk.News.Id.Value);
        ClientScript.RegisterStartupScript(GetType(), "aa", "alert('修改成功');document.location='Default.aspx'", true);
    }
}